<template>
  <!-- 角色列表 -->
  <div>
    <page-header-wrapper />
    <a-card style="margin-top: 10px" :loading="loading">
      <a-input-search placeholder="请输入权限名称" v-model="searchForm.user_role_name" @search="search" style="width: 240px; margin-bottom: 10px" />
      <a-button @click="visible = true" style="float: right" type="primary">新增权限</a-button>
      <a-table
        :columns="columns"
        :data-source="data"
        bordered
        :rowKey="record=>record.user_role_id"
        :pagination="pagination"
      >
        <template slot="operation" slot-scope="text, record">
          <div class="editable-row-operations">
            <a-popconfirm :title="$t('form.basic-form.confirm.delete')" @confirm="() => deleteSysRoleInfo(record)">
              <a>删除</a>
            </a-popconfirm>
          </div>
        </template>
      </a-table>
    </a-card>
    <a-modal v-model="visible" :footer="null" title="编辑框">
      <a-form
        :form="form"
        :loading="formLoading"
      >
        <a-form-item
          label="权限名称"
        >
          <a-input
            v-decorator="[
              'user_role_name',
              {rules: [{ required: true, message: '权限名称不能为空' }], validateTrigger: 'blur', initialValue: editForm.user_role_name}
            ]"
          />
        </a-form-item>
        <a-form-item
          label="权限类型"
        >
          <a-input
            v-decorator="[
              'user_role_path',
              {rules: [{ required: true, message: '权限类型不能为空' }], validateTrigger: 'blur', initialValue: editForm.user_role_path}
            ]"
          />
        </a-form-item>
        <a-form-item
          label="权限描述"
        >
          <a-input
            v-decorator="[
              'user_role_description',
              {rules: [{ required: true, message: '权限描述不能为空' }], validateTrigger: 'blur', initialValue: editForm.user_role_description}
            ]"
          />
        </a-form-item>
        <a-form-item style="margin-top:24px">
          <a-button
            size="large"
            type="primary"
            @click="handleSubmit"
          >{{ $t('submit') }}</a-button>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { PageHeaderWrapper } from '@ant-design-vue/pro-layout'
import {
  addSysRoleInfo,
  getSysUserRoleList,
  deleteSysRole
} from '@/api/user'

const columns = [
  {
    title: 'user_role_name',
    dataIndex: 'user_role_name',
    width: '10%',
    scopedSlots: { customRender: 'user_role_name' }
  },
  {
    title: 'user_role_path',
    dataIndex: 'user_role_path',
    width: '10%',
    scopedSlots: { customRender: 'user_role_path' }
  },
  {
    title: 'user_role_description',
    dataIndex: 'user_role_description',
    width: '10%',
    scopedSlots: { customRender: 'user_role_description' }
  },
  {
    title: 'operation',
    dataIndex: 'operation',
    width: '15%',
    scopedSlots: { customRender: 'operation' }
  }
]

export default {
  name: 'RoleList',
  components: {
    PageHeaderWrapper
  },
  data () {
    return {
      searchForm: {
        page: 1,
        user_role_name: ''
      },
      columns,
      form: this.$form.createForm(this),
      data: [],
      loading: true,
      visible: false,
      formLoading: false,
      editForm: {
        user_role_id: 0,
        user_role_name: '',
        user_role_path: '',
        user_role_description: ''
      },
      pagination: {
        total: 0,
        onChange: (page) => {
          this.searchForm.page = page
          this.getSysUserRoleListInfo()
        }
      }
    }
  },
  mounted () {
    this.getSysUserRoleListInfo()
  },
  watch: {
    visible (value) {
      if (value) {
        this.editForm = {
          user_role_id: 0,
          user_role_name: '',
          user_role_path: '',
          user_role_description: ''
        }
      }
    }
  },
  methods: {
    search () {
      // code ...
      this.searchForm.page = 1
      this.getSysUserRoleListInfo()
    },
    getSysUserRoleListInfo () {
      getSysUserRoleList(this.searchForm).then(res => {
        this.data = res.items
        this.pagination.total = res.count ? res.count : 0
        this.loading = false
      })
    },
    deleteSysRoleInfo (data) {
      deleteSysRole(data).then(res => {
        this.$message[res.code === 200 ? 'success' : 'warning'](res.msg)
        if (res.code === 200) {
          this.getUserRoleListInfo()
        }
      })
    },
    handleSubmit (e) {
      this.formLoading = true
      e.preventDefault()
      const {
        form: { validateFields }
      } = this
      validateFields(Object.keys(this.editForm), { force: true }, (err, values) => {
        if (!err) {
          this.editForm = Object.assign(this.editForm, values)
          addSysRoleInfo(this.editForm).then(res => {
            this.$message[res.code === 200 ? 'success' : 'warning'](res.msg)
            this.formLoading = this.visible = false
            if (res.code === 200) {
              this.data.unshift(res.items)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
